package com.rfid.a252robot.base;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.rfid.a252robot.event.StopEvent;
import com.rfid.common.log.LogUtils;
import com.rfid.mqtt.MqttManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public abstract class BaseActivity<T extends BasePresenter>  extends BaseTitleActivity {

    protected T mPresenter;
    CountDownTimer mCountDownTimer;

    static Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            System.out.println("resumeRobot");
            LogUtils.v("resumeRobot");
            MqttManager.INSTANCE.resumeRobot();
            return true;
        }
    });

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = getPresenter();
        if (mPresenter != null)
            mPresenter.onAttach(this);

        EventBus.getDefault().register(this);

        int time = getCountDownTime();
        if (time > 0) {
            mCountDownTimer = new CountDownTimer(time, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    onTimeTick(millisUntilFinished);
                }

                @Override
                public void onFinish() {
                    onTimeFinish();
                }
            };
            mCountDownTimer.start();
        }
        mHandler.removeMessages(0);
        mHandler.sendEmptyMessageDelayed(0,getWaitTime());
    }

    protected abstract int getCountDownTime();

    protected void onTimeTick(long millisUntilFinished){

    }

    protected void onTimeFinish() {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setSystemUIVisible(false);
    }

    protected void setSystemUIVisible(boolean show) {
//        if (true) {
//            return;
//        }
        if (show) {
            int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            uiFlags |= 0x00001000;
            getWindow().getDecorView().setSystemUiVisibility(uiFlags);
        } else {
            int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
            uiFlags |= 0x00001000;
            getWindow().getDecorView().setSystemUiVisibility(uiFlags);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        System.out.println("dispatchTouchEvent = " + ev.getAction());
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            System.out.println("pauseRobot");
            MqttManager.INSTANCE.pauseRobot();
            if (isResetCDTime()) {
                resetCountDownTime();
            }
            mHandler.removeMessages(0);
            mHandler.sendEmptyMessageDelayed(0,getWaitTime());
        }
        return super.dispatchTouchEvent(ev);
    }

    private boolean isResetCDTime() {
        return true;
    }

    protected void resetCountDownTime(){
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = new CountDownTimer(60000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    onTimeTick(millisUntilFinished);
                }

                @Override
                public void onFinish() {
                    onTimeFinish();
                }
            };
            mCountDownTimer.start();
        }
    }

    protected int getWaitTime() {
        return 10000;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void stopEvent(StopEvent event) {
        LogUtils.v("stopEvent pauseRobot");
        MqttManager.INSTANCE.pauseRobot();
        mHandler.removeMessages(0);
        mHandler.sendEmptyMessageDelayed(0,10000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detach();
        EventBus.getDefault().unregister(this);
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
        }
    }

    public abstract T getPresenter();
}
